<style lang="less">
@import "../../../assets/style/photo.less";
</style>

<template>
    <mxq-router-layout>
        <div class="photo">
            <Head slot="header" @headerClick="headerClick($event)">
                <div slot="headerTitle">
                    <span>{{title}}杆塔</span>
                </div>
                
                <div v-show="isEnd" slot="headerRight">
                    <i class="iconfont icon-duigou"/>
                    <!--<van-icon name="success" ></van-icon>-->
                </div>
                <div v-show="!isEnd" slot="headerRight">
                    <i class="iconfont icon-shouye"/>
                    <!--<van-icon class="home" name="home"/>-->
                </div>
            </Head>
            
            <span class="iconCrad" v-show="isClock">
                <img
                    class="iconRight"
                    src="../../../assets/img/changguixunshi/icon_rw.png"
                    @click="punchCard"/>
                <em class="iconText">打卡</em>
            </span>
            <span class="iconBox" v-show="isEnd">
                <img
                    class="iconRight"
                    src="../../../assets/img/changguixunshi/icon_rw.png"
                    @click="cancelTask"/>
                <em class="iconText">取消任务</em>
            </span>
            
            <!-- 拍照 start -->
            <take-photo
                :pohtoParam="pohtoParam"
                :photoLists="photoLists"
                :pohtoShow="pohtoShow"
                @quexian_data="initQuanxian"
                :isEnd = "isEnd"/>
            <!-- 拍照 end -->
            
            <van-row class="quexian_item">
                <van-col span="24">
                    <p class="photo_item tit_item clearfix">
                        <label for="架空线路缺陷核查"><em>*</em>架空线路缺陷核查</label>
                    </p>
                </van-col>
            </van-row>
            
            <!-- 八大线路缺陷 开始 -->
            <quexian-list
                :listLeftImg="listLeftImg" 
                :type_arr="type_arr"
                @quexianClick="quexianClick($event)"/>
            <!-- 八大线路缺陷 结束 -->
            
            <!--一键告警-->
            <img src="../../../assets/img/icon_sos.png"
                class="icon_sos"
                @click="getSos"
                alt=""/>
            
            <div class="btns">
                <van-tabbar>
                    <van-tabbar-item>
                        <van-button size="small" class="btn_l" @click="goYhList">隐患情况</van-button>
                    </van-tabbar-item>
                    <van-tabbar-item>
                        <van-button size="small" class="btn_r" @click="goClArea">测量记录</van-button>
                    </van-tabbar-item>
                </van-tabbar>
            </div>
            <!-- 弹出层 开始 -->
            <!--<div class="pop_up">
                <van-popup v-model="show">
                    <span class="pop_txt">
                        当前在此杆塔100米范围内所停留时间为0秒，不满足巡视任务杆塔180秒停留作业，是否提交巡视不到位作业？
                    </span>
                    <van-button size="small" class="btn_l">是</van-button>
                    <van-button size="small" class="btn_r">否</van-button>
                </van-popup>
            </div>-->
            <!-- 弹出层 结束 -->
        </div>
    </mxq-router-layout>
</template>

<script>
import merge from 'webpack-merge';
import Head from '../../../components/head';
import TakePhoto from '../../components/taskTake-photo'
import QuexianList from '../../../components/quexian';
import iconHis from '../../../assets/img/changguixunshi/icon_histroy.png'
import { callData,fileData } from '../../../axios/axios'
import { getFlatternDistance } from '../../../libs/util'
import { getLocation } from '../../../api/android'
import { 
    Row,
    Col,
    Tabbar,
	TabbarItem,
    Button,
    Icon,
    Popup,
    Toast,
    Dialog
} from 'vant';

export default {
    components: {
        Head,
        TakePhoto,
        QuexianList,
        [Row.name]: Row,
        [Col.name]: Col,
        [Tabbar.name]:Tabbar,
        [TabbarItem.name]:TabbarItem,
        [Button.name]:Button,
        [Icon.name]:Icon,
        [Popup.name]:Popup,
        [Toast.name]:Toast,
        [Dialog.name]:Dialog
    },
    data() {
        return {
        	title:this.$route.query.towerName,
            username: '',
            iconText:'打卡',
            show: false,
            isEnd: true,   //取消任务图标
            isClock: true,//是否打卡
            // 历史图标
            listLeftImg:[iconHis,iconHis,iconHis,iconHis,iconHis,iconHis,iconHis,iconHis,iconHis],
            pohtoParam: {
            	taskId: this.$route.query.taskId,
            	towerTaskId: this.$route.query.towerTaskId,
            	towerId: this.$route.query.cmTowerId,
                status: this.$route.query.status
            },
            pohtoShow: {
            	type: 'xljx',
            	img: true
            },
            photoLists: [
                {
                    title: "杆塔牌拍照",
                    photo:[]
                },
                {
                    title: "杆基拍照",
                    photo:[]
                },
                {
                    title: "全塔拍照",
                    photo:[]
                },
                {
                    title: "塔头拍照",
                    photo:[]
                },
                {
                    title: "大号侧同拍照",
                    photo:[]
                },
                {
                    title: "小号侧同拍照",
                    photo:[]
                }
            ],
            type_arr: {},
            imgLength: 0,
            jdwdMeg:{//非安卓环境下参数
            	lon:'00.000000',
            	lat:'00.000000',
            	address:''
            },
            lon: '',    //安卓经度
            lat: '',    //安卓纬度
        }
    },
    mounted() {
        // 是否打卡判断：如果已经打卡了，则隐藏该标签
    	var _this = this;
		if(_this.$route.query.punchtype == 1){
            _this.isClock = false;
		}else{
            _this.isClock = true;
		}
		this.isShow();
    },
    methods: {
		// 头部两侧按钮点击事件
		headerClick(e){
			if(this.isEnd){
				this.endGanTaTask() //结束该杆塔任务
			}else{
				let query = {}
				this.$router.push({
					name:'index',
					query: query
				})
			}
        },
        // 初始化缺陷数据
        initQuanxian(e){
            this.type_arr = e;
        },
        // 八大缺陷的点击事件
		quexianClick(e){
			this.$router.push({
				name:'xljx_quexianList',
				query : {
                    name: e.allName,
                    flag: e.FLAG,
                    defectType: e.index,
					towerId: this.$route.query.cmTowerId,
					punchtype: this.$route.query.punchtype,
					lineId: this.$route.query.lineId,
					lineName: this.$route.query.lineName,
					towerName: this.$route.query.towerName,
					taskId: this.$route.query.taskId,
					lineTowerId:this.$route.query.lineTowerId,
                    towerStatus: this.$route.query.towerStatus,
                    towerTaskId: this.$route.query.towerTaskId,
                    index: this.$route.query.index,
                    lineName:this.$route.query.lineName,
					towerName:this.$route.query.towerName
	        	}
			})
        },
        goYhList() {
            this.$router.push({
				name:'xljx_yinhuanList',
				query : {
					towerId:this.$route.query.cmTowerId,
					punchtype:this.$route.query.punchtype,
					lineId:this.$route.query.lineId,
					lineName:this.$route.query.lineName,
					towerName:this.$route.query.towerName,
					taskId :this.$route.query.taskId,
					lineTowerId :this.$route.query.lineTowerId,
                    towerTaskId:this.$route.query.towerTaskId,
                    towerStatus: this.$route.query.towerStatus,
                    gantaStatus:this.$route.query.gantaStatus
	        	}
			})
        },
        goClArea() {
            this.$router.push({
				name:'xljx_ceLiang',
				query : {
					towerId:this.$route.query.cmTowerId,
					taskId :this.$route.query.taskId,
					lineTowerId :this.$route.query.lineTowerId,
                    towerTaskId:this.$route.query.towerTaskId,
                    gantaStatus:this.$route.query.gantaStatus
	        	}
			})
        },
        // 一键告警
        getSos(){
            Dialog.confirm({
                title: '告警',
                message: '确定要发出告警吗？'
            }).then(() => {
                // on confirm
                if(window.Cordova){
                    getLocation(res => {
                    		var params = {};
                        params.jd = res.data.lon;
                        params.wd = res.data.lat;
                        params.alarmAddress=res.data.address;  // 当前的地理位置
                        callData('/statisServer/CmAlarm/saveCmAlarm', 'post', params).then(res => {
                            if(res.success){
                            		Toast('告警发送成功');
                            }
                            else {
                            		Toast('告警发送失败');
                        		}
                        })
                    })
                }
                else{
                    var params = {};
                    params.jd = this.jdwdMeg.lon;            //经度 
                    params.wd = this.jdwdMeg.lat;            //纬度
                    params.alarmAddress = this.jdwdMeg.address;
                    callData('/statisServer/CmAlarm/saveCmAlarm', 'post', params).then(res => {
                        if(res.success){
                        	Toast('告警发送成功');
                        }
                        else {
                            Toast('告警发送失败');
                        }
                    })
                }
            }).catch(() => {
                // on cancel
            });
        },
        //打卡
        punchCard(){
        	var _this = this;
        	//获取杆塔位置
        	callData('/jxtaskServer/JxTowerTask/findTowerJdWd', 'get', {
        		cmTowerId:_this.$route.query.cmTowerId
        	}).then(res1 => {
        		if(res1.success){
        			_this.getAndrioLocation( res1.data);
        		}
			})
        },
        getAndrioLocation (locate) {
        	var _this = this;
        	if (window.Cordova) {
        		//调安卓进行定位
        		getLocation(res => {
					let distance = getFlatternDistance(Number(res.data.lat), Number(res.data.lon), Number(locate.WD), Number(locate.JD));
					if (_this.$route.query.punchtype != 1) {//未打卡
			        	callData('/jxtaskServer/JxTowerTask/updatePunch', 'put', {
			        		punchDistance:distance,//打卡距离
			        		jd:res.data.lon,//经度
			        		wd:res.data.lat,//纬度
			        		cmTowerId:_this.$route.query.cmTowerId,//物理杆塔IDcmTowerId
			        		taskId:_this.$route.query.taskId//任务ID
			        	}).then(res => {
			        		if(res.success){
			        			//修改原有参数        
								_this.$router.push({
								    query:merge(this.$route.query,{'punchtype':'1'})
								})
								_this.isClock = false;
			        		}
		                    Toast(res.data);
						})
		        	}
    			})
        	}else{
        		if (_this.$route.query.punchtype != 1) {
	     			var params = {
		        		punchDistance:2000,//打卡距离
	        			jd:113.663221,//经度
	       				wd:34.7568711,//纬度
		        		cmTowerId:_this.$route.query.cmTowerId,//物理杆塔IDcmTowerId
		        		taskId:_this.$route.query.taskId//任务ID
		        	}
		        	callData('/jxtaskServer/JxTowerTask/updatePunch', 'put', params).then(res => {
	                    if(res.success){
							//修改原有参数        
							_this.$router.push({
							    query:merge(this.$route.query,{'punchtype':'1'})
							})
							_this.isClock = false;
		        		}
	                    Toast(res.data);
					})
        		}
        	}
        },
        // 结束该杆塔任务
        endGanTaTask(){
            const _this = this
            var params = {
                cmTowerId: this.$route.query.cmTowerId,
                taskId: this.$route.query.taskId,
                towerTaskId: this.$route.query.towerTaskId
            }
            callData('/jxtaskServer/JxTowerTask/endJxTowerTask', 'put', params).then(res => {
                // 结束杆塔任务后返回列表页
                if(res.success) {
                    Toast(res.data.Message);
                    this.$router.push({
						name:'xljx_gantaXunshi',
						query: {
							taskId: this.$route.query.taskId,
							page: this.$route.query.page,
							size: this.$route.query.size,
							index: this.$route.query.index
						}
					})
                }
                else {
                    Toast(res.data);
                }
            })
        },
        //取消任务
        cancelTask(e) {
        	var params ={
        		acceptTask:0,//0表示取消接单
				towerTaskId: this.$route.query.towerTaskId
			}
        	callData('/jxtaskServer/JxTowerTask/updateJdUser', 'put', params).then(res => {
                Toast(res.data);
                if(res.success){
                	//this.$router.go(-1)
                	this.$router.push({
                      name:'xljx_gantaXunshi',
                      query: this.$route.query
                   })
                }
			})
        },
        isShow(){
            if(this.$route.query.towerStatus==2){
                this.isEnd = false;
            }
        }
    }
}
</script>
